package com.jftone.bh.app.service;

import com.jftone.bh.app.config.SysConst;
import com.jftone.bh.app.model.NtbNewslist;
import com.jftone.bh.app.model.SysAdminRole;
import com.jftone.bh.app.model.TmpImg;
import org.apache.log4j.Logger;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Service;
import org.jftone.annotation.Transactional;
import org.jftone.dao.Dao;
import org.jftone.exception.DaoException;
import org.jftone.util.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;


@Service
public class NewsService {
	
	private Logger logger = Logger.getLogger(NewsService.class);
	
	@Autowired
	private Dao dao;

	@Autowired
	private OSSFileManagerService ossService;

	public List<IData<String, Object>> queryPageList(IData<String, Object> data, Page page) throws Exception {
		List<IData<String, Object>> list = null;
		String newsTitle = data.getString("newsTitle", "");
		String ctype = data.getString("ctype", "");
		IData<String, Object> param = new DataMap<String, Object>();
		if(!StringUtil.isBlank(newsTitle)){
			param.put("NEWS_TITLE", "%" + newsTitle + "%");
		}
		if(!StringUtil.isBlank(ctype)){
			param.put("CTYPE", ctype);
		}
		list = dao.query("queryNewsList", param, page);
		return list;
	}

	@Transactional
	public int saveModel(IData<String, Object> data, boolean updateFlag) throws DaoException {
		NtbNewslist obj = new NtbNewslist();
		obj.setIsTop(data.getInt("isTop", 0));
		obj.setCtype(data.getInt("ctype", 1));
		obj.setSort(data.getInt("isTop", 2));
		obj.setAuthor(data.getString("author", ""));
		obj.setContent(data.getString("content", ""));
		obj.setDescribes(data.getString("describes", ""));
		obj.setMediaUrl(data.getString("mediaUrl"));
		obj.setNewsTitle(data.getString("newsTitle"));

		String imgId = data.getString("imgId", "");
		String oldImgSrc = data.getString("oldImgSrc", "");
		IData<String, Object> updateData = new DataMap<String, Object>();
		IData<String,Object> inData = new DataMap<String,Object>();
		if(updateFlag){
			obj.setId(data.getInt("newsId"));
			dao.update(obj);
			if(!data.getString("mediaUrl").equals(oldImgSrc) && !StringUtil.isEmpty(oldImgSrc)) {
				updateData.put("removeTag", SysConst.FILE_REMOVE_TAG_YES);
				inData.put("imgSrc", oldImgSrc);
				dao.update(TmpImg.class, updateData, inData);
				ossService.delete(oldImgSrc);
			}

		}else{
			obj.setAddDate(DateUtil.format(new Date(),DateUtil.FMT_DATETIME));
			obj.setHits(0);
			dao.save(obj);
		}
		if(!StringUtil.isBlank(imgId) && StringUtil.isNumber(imgId)) {
			inData.clear();
			updateData.clear();
			updateData.put("removeTag", SysConst.FILE_REMOVE_TAG_USE);
			inData.put("id", Long.parseLong(imgId));
			dao.update(TmpImg.class, updateData, inData);
		}
		return 0;
	}

	public NtbNewslist getNtbNewslist(IData<String, Object> inData) throws DaoException {

		return dao.get(NtbNewslist.class,inData);
	}
	@Transactional
	public int delete(int newsId) throws DaoException {

		IData<String, Object> param = new DataMap<String, Object>();
		param.put("id", newsId);
		NtbNewslist ntbNewslist = dao.get(NtbNewslist.class, param);
		if(ntbNewslist == null){
			return 1;
		}
		if(!StringUtil.isBlank(ntbNewslist.getMediaUrl())){

			param.clear();
			param.put("imgSrc", ntbNewslist.getMediaUrl());
			dao.delete(TmpImg.class, param);
			ossService.delete(ntbNewslist.getMediaUrl());
		}
		dao.delete(ntbNewslist);
		return 0;
	}
}
